package com.chen10.gobang.common.utils;

import com.chen10.gobang.common.constants.GameConstants;
import com.chen10.gobang.model.vo.PlayerInfoVO;
import com.chen10.gobang.ws.pojo.MoveData;
import com.chen10.gobang.ws.pojo.Room;

import java.util.ArrayList;
import java.util.List;

public class GameUtils {

    /**
     * 检查是否胜利，并返回五颗子的坐标列表（如果有的话）
     *
     * @param board 棋盘
     * @param row   行坐标
     * @param col   列坐标
     * @return 如果胜利则返回包含五颗子坐标的列表，否则返回null
     */
    public static List<int[]> checkWin(int[][] board, int row, int col) {
        int value = board[row][col];
        // 这里简化处理，实际应检查四个方向：水平、垂直、两个对角线
        List<int[]> winPositions = null;
        if (checkDirection(row, col, 0, 1, value, board)) { // 水平
            winPositions = getWinningPositions(row, col, 0, 1, board);
        } else if (checkDirection(row, col, 1, 0, value, board)) { // 垂直
            winPositions = getWinningPositions(row, col, 1, 0, board);
        } else if (checkDirection(row, col, 1, 1, value, board)) { // 右下斜
            winPositions = getWinningPositions(row, col, 1, 1, board);
        } else if (checkDirection(row, col, -1, 1, value, board)) { // 左下斜
            winPositions = getWinningPositions(row, col, -1, 1, board);
        }
        return winPositions;
    }
    /**
     * 检查是否有玩家胜利
     *
     * @param board 棋盘
     * @return 如果有胜利的玩家返回true，否则返回false
     */
    public static boolean checkWin(int[][] board) {
        // 遍历棋盘的每个位置
        for (int row = 0; row < GameConstants.BOARD_SIZE; row++) {
            for (int col = 0; col < GameConstants.BOARD_SIZE; col++) {
                // 如果当前位置有棋子，检查四个方向
                if (board[row][col] != 0) {
                    int value = board[row][col];

                    // 检查四个方向：水平、垂直、斜线（右下斜和左下斜）
                    if (checkDirection(row, col, 0, 1, value, board) ||  // 水平
                            checkDirection(row, col, 1, 0, value, board) ||  // 垂直
                            checkDirection(row, col, 1, 1, value, board) ||  // 右下斜
                            checkDirection(row, col, -1, 1, value, board)) { // 左下斜
                        return true;  // 找到五颗相同的棋子，返回胜利
                    }
                }
            }
        }
        return false;  // 如果遍历完没有找到胜利者，返回false
    }
    // 检查不同方向
    private static boolean checkDirection(int row, int col, int dRow, int dCol, int value, int[][] board) {
        int count = 1; // 包括自身
        for (int i = 1; i < 5; ++i) {
            int newRow = row + dRow * i;
            int newCol = col + dCol * i;
            if (newRow >= 0 && newRow < GameConstants.BOARD_SIZE && newCol >= 0 && newCol < GameConstants.BOARD_SIZE && board[newRow][newCol] == value) {
                count++;
            } else {
                break;
            }
        }
        for (int i = 1; i < 5; ++i) {
            int newRow = row - dRow * i;
            int newCol = col - dCol * i;
            if (newRow >= 0 && newRow < GameConstants.BOARD_SIZE && newCol >= 0 && newCol < GameConstants.BOARD_SIZE && board[newRow][newCol] == value) {
                count++;
            } else {
                break;
            }
        }
        return count >= 5;
    }

    // 获取胜利的五颗子的坐标
    private static List<int[]> getWinningPositions(int row, int col, int dRow, int dCol, int[][] board) {
        List<int[]> positions = new ArrayList<>();
        int value = board[row][col];

        // 向一个方向寻找直到找到不是value的棋子或者边界
        for (int i = 0; i < 5; ++i) {
            int newRow = row - dRow * i;
            int newCol = col - dCol * i;
            if (newRow >= 0 && newRow < GameConstants.BOARD_SIZE && newCol >= 0 && newCol < GameConstants.BOARD_SIZE && board[newRow][newCol] == value) {
                positions.add(new int[]{newRow, newCol});
            } else {
                break;
            }
        }

        // 确保只有五个位置被添加
        if (positions.size() < 5) {
            for (int i = 1; positions.size() < 5; ++i) {
                int newRow = row + dRow * i;
                int newCol = col + dCol * i;
                if (newRow >= 0 && newRow < GameConstants.BOARD_SIZE && newCol >= 0 && newCol < GameConstants.BOARD_SIZE && board[newRow][newCol] == value) {
                    positions.add(new int[]{newRow, newCol});
                } else {
                    break;
                }
            }
        }

        return positions.size() == 5 ? positions : null;
    }


    /**
     * 获取下一个id
     *
     * @param playerInfoList
     * @param currentPlayerId
     * @return
     */
    public static Integer getNextPlayerId(List<PlayerInfoVO> playerInfoList, Integer currentPlayerId) {
        return playerInfoList.get(0).getUserId().equals(currentPlayerId) ? playerInfoList.get(1).getUserId() : playerInfoList.get(0).getUserId();
    }

    /**
     * 检查是否有效移动
     *
     * @param board
     * @param row
     * @param col
     * @return
     */
    public static boolean isValidMove(int[][] board, Integer boardSize, int row, int col) {
        // 简单的边界检查和是否为空位检查BOARD_SIZE
        return row >= 0 && row < boardSize && col >= 0 && col < boardSize && board[row][col] == 0;
    }

    /**
     * 将MoveData格式化为字符串
     * @param moveData
     * @return
     */
    public static String formatMoveData(MoveData moveData) {
        return moveData.getRow() + "," + moveData.getCol() + ",";
    }


    /**
     * 判断游戏是否为平局（棋盘已满且无胜利者）
     *
     * @param board 棋盘
     * @return 如果棋盘已满且无胜利者则返回true，否则返回false
     */
    public static boolean checkBoardFull(int[][] board) {
        for (int row = 0; row < GameConstants.BOARD_SIZE; row++) {
            for (int col = 0; col < GameConstants.BOARD_SIZE; col++) {
                if (board[row][col] == 0) { // 假设0代表空位
                    return false; // 如果找到至少一个空位，则没满
                }
            }
        }
        return true; // 棋盘已满
    }
}


